package com.atguigu.gulimall.search.threadFeature;

import java.util.concurrent.*;

/**
 * @description:异步编排测试
 * @title: ThreadFuture
 * @Author FangDaDa
 * @Date: 2021/7/27 19:44
 * @Version 1.0
 */
public class ThreadFuture {

    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        System.out.println("main......start.....");

        // 方法执行完成后的处理
//         CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//             System.out.println("当前线程：" + Thread.currentThread().getId());
//             int i = 10 / 2;
//             System.out.println("运行结果：" + i);
//             return i;
//         }, executor).handle((result,thr) -> {
//             if (result != null) {
//                 return result * 2;
//             }
//             if (thr != null) {
//                 System.out.println("异步任务成功完成了...结果是：" + result + "异常是：" + thr);
//                 return 0;
//             }
//             return 0;
//         });

        /**
         * 线程串行化
         * 1、thenRunL：不能获取上一步的执行结果
         * 2、thenAcceptAsync：能接受上一步结果，但是无返回值
         * 3、thenApplyAsync：能接受上一步结果，有返回值
         *
         */
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).thenApplyAsync(res -> {
//            System.out.println("任务2启动了..." + res);
//            return "Hello" + res;
//        }, executor);

        // 两个都要完成
//        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务1线程开始,当前任务1线程id:" + Thread.currentThread().getId());
//            int i = 10 / 4;
//            System.out.println("任务1线程结束，结果是：" + i);
//            return i;
//        }, executor);
//
//        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2线程开始,当前任务2线程id:" + Thread.currentThread().getId());
//            try {
//                Thread.sleep(3000);
//                System.out.println("任务2线程结束");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            return "Hello";
//        }, executor);

//        future01.runAfterBothAsync(future02,()->{
//            System.out.println("任务3开始");
//        },executor);

//        future01.thenAcceptBothAsync(future02,(f1,f2)->{
//            System.out.println("任务3开始....之前的结果:" + f1 + "--》" + f2);
//        },executor);

//        CompletableFuture<String> future03 = future01.thenCombineAsync(future02, (f1, f2) -> {
//            return f1 + ":" + f2 + "->haha";
//        }, executor);

        // 两个任务，只要有一个完成，我们就去执行任务3
//        future01.runAfterEitherAsync(future02,()->{
//            System.out.println("任务3开始之前的结果");
//        },executor);

//        future01.acceptEitherAsync(future02,(res)->{
//            System.out.println("任务3开始之前的结果是:" + res);
//        },executor);

//        CompletableFuture<String> future03 = future01.applyToEitherAsync(future02, res -> {
//            System.out.println("任务3开始....之前的结果:" + res);
//            return res.toString() + "hhhh";
//        }, executor);

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品图片");
            return "小米.jpg";
        }, executor);

        CompletableFuture<String> futureName = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000);
                System.out.println("查询商品名");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "小米K40";
        }, executor);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品介绍");
            return "这是小米11手机....";
        }, executor);

        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureName, futureDesc);
        // 等待所有结果都完成
        anyOf.get();

        System.out.println("main......end....." +  anyOf.get());

//        System.out.println("main......end....."
//                + "图片：" + futureImg.get()
//                + "商品名:" + futureName.get()
//                + "商品介绍:" + futureDesc.get());

    }

}
